﻿//Frank Baumgartner sample-accurate timing hack - hopefully will port to a class (Scope problems)
//-----------------------------------------------------------------------------------------------

//variables necessary for scope of recursive onsoundcomplete function (a bit iffy - but scoping problems with timing hack as a class)
var bpm:Number = 140; //attach variables to sound object for recursive access.
var SPD:Number = 12; //something to do with ticks
var tmr:Number = 0;
var stp:Number =0; 
var seq:Array = new Array(); // container for a seq of audio data (stores strings which reference sounds in library)
var len:Number = 8; //length of sequence

//initialise the silent 1sample clip
createEmptyMovieClip("sync_mc", 3000);

//-loadsounds-----------------------------------------------------------

	var sync:Sound = new Sound(sync_mc);
	sync.attachSound("sync");
	sync.setVolume(0);
	
	//-level sounds-----------------------------
	
	//-808------------------------------------------
	
	createEmptyMovieClip("sample0_mc", 3001);
	var kick:Sound = new Sound(sample0_mc);
	kick.attachSound("kick");
	
	createEmptyMovieClip("sample1_mc", 3002);
	var snare:Sound = new Sound(sample1_mc);
	snare.attachSound("snare");
	
	createEmptyMovieClip("sample2_mc", 3003);
	var high:Sound = new Sound(sample2_mc);
	high.attachSound("high");
	
	createEmptyMovieClip("sample3_mc", 3004);
	var crash:Sound = new Sound(sample3_mc);
	crash.attachSound("crash");
	
	//-hardcore-------------------------------------
	
	createEmptyMovieClip("sample12_mc", 3012);
	var hc_kick:Sound = new Sound(sample12_mc);
	hc_kick.attachSound("hc_kick");
	
	createEmptyMovieClip("sample13_mc", 3013);
	var hc_snare:Sound = new Sound(sample13_mc);
	hc_snare.attachSound("hc_snare");
	
	createEmptyMovieClip("sample14_mc", 3014);
	var hc_high:Sound = new Sound(sample14_mc);
	hc_high.attachSound("hc_high");
	
	createEmptyMovieClip("sample15_mc", 3015);
	var hc_crash:Sound = new Sound(sample15_mc);
	hc_crash.attachSound("hc_crash");
	
	//-amen---------------------------------------
	
	createEmptyMovieClip("sample16_mc", 3016);
	var a_kick:Sound = new Sound(sample16_mc);
	a_kick.attachSound("a_kick");
	
	createEmptyMovieClip("sample17_mc", 3017);
	var a_snare:Sound = new Sound(sample17_mc);
	a_snare.attachSound("a_snare");
	
	createEmptyMovieClip("sample18_mc", 3018);
	var a_high:Sound = new Sound(sample18_mc);
	a_high.attachSound("a_high");
	
	createEmptyMovieClip("sample19_mc", 3019);
	var a_crash:Sound = new Sound(sample19_mc);
	a_crash.attachSound("a_crash");
	
	//--------------------------------------------------------
	// BACKGROUND LOOP CONTAINER THIS IS SET BY ACCESSOR!!!
	createEmptyMovieClip("sample4_mc",3005);
	var bgm:Sound = new Sound(sample4_mc);
	//bg1.attachSound("bg_level1");
	
	//---------------------------------------------------------
	
	//effects samples
	
	createEmptyMovieClip("sample6_mc",3007);
	var correct:Sound = new Sound(sample6_mc);
	correct.attachSound("correct");

	createEmptyMovieClip("sample7_mc",3008);
	var incorrect:Sound = new Sound(sample7_mc);
	incorrect.attachSound("incorrect");
	
	createEmptyMovieClip("sample8_mc",3009);
	var die:Sound = new Sound(sample8_mc);
	die.attachSound("pop");
	
	createEmptyMovieClip("sample9_mc",3010);
	var thump:Sound = new Sound(sample9_mc);
	thump.attachSound("thud");
	
	createEmptyMovieClip("sample10_mc",3011);
	var smash:Sound = new Sound(sample10_mc);
	smash.attachSound("smash");
	
//---------------------------------------------------------------------

sync.onSoundComplete = function() //sync the sample playback according to the amount of buffers which have elapsed 
{
	sync.start(0, 1); //44100*SPD/(BPM*2/5) - 44100*23.2*3/1000 );
	
	tmr -= 2048/44100; // buffers out of a second (in samples)
	
	if (tmr > 0) {return;}; //kick back untill the alloted tick has elapsed
	
	tmr += SPD/(bpm*2/5); // tick value for tracker (in samples)
	var offset:Number = SPD/(bpm*2/5) - tmr; //amount to offset sample by according to gap in accuracy between the projected and actual tick value in samples.
	
	eval(seq[stp]).start(offset, 1); //play the sample
	
	//beta code
	
	if(stp == 0)
	{
		bgm.start(offset,1);//test cpu load on two sounds at once
	}
	
	stp = (stp + 1) % len; //increment the seqr stp
}
sync.start(0, 1); // kick off recursion

//-----------------------------------------------------------------------------------------------

kill_sequencer = function()
{
	trace("killing off sequencer");
	delete sync.onSoundComplete;//kill the complete function
	sync_mc.removeMovieClip(); //kill clip and .: timing loop .: playback
	//kill all variables used
	delete seq; //kill array
	delete bpm; 
	delete SPD; 
	delete tmr;
	delete stp; 
	delete len;
}
//so level can set dynamically
set_bpm = function(new_bpm:Number)
{
	bpm = new_bpm;
}
set_len = function(new_len:Number)
{
	len = new_len;
}
set_bgm = function(new_bgm:String)
{
	bgm.attachSound(new_bgm);
}